package cn.iocoder.yudao.module.fengdong.smartelderly.service.accessrecord;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.fengdong.smartelderly.controller.admin.accessrecord.vo.*;
import cn.iocoder.yudao.module.fengdong.smartelderly.dal.dataobject.accessrecord.AccessRecordDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.fengdong.smartelderly.convert.accessrecord.AccessRecordConvert;
import cn.iocoder.yudao.module.fengdong.smartelderly.dal.mysql.accessrecord.AccessRecordMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.fengdong.smartelderly.enums.ErrorCodeConstants.*;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;

/**
 * 门禁记录 Service 实现类
 *
 * @author 沣东养老院管理员
 */
@Service
@Validated
public class AccessRecordServiceImpl implements AccessRecordService {

    @Resource
    private AccessRecordMapper accessRecordMapper;

    @Override
    public Long createAccessRecord(AccessRecordCreateReqVO createReqVO) {
        // 插入
        AccessRecordDO accessRecord = AccessRecordConvert.INSTANCE.convert(createReqVO);
        accessRecordMapper.insert(accessRecord);
        // 返回
        return accessRecord.getId();
    }

    @Override
    public void updateAccessRecord(AccessRecordUpdateReqVO updateReqVO) {
        // 校验存在
        validateAccessRecordExists(updateReqVO.getId());
        // 更新
        AccessRecordDO updateObj = AccessRecordConvert.INSTANCE.convert(updateReqVO);
        accessRecordMapper.updateById(updateObj);
    }

    @Override
    public void deleteAccessRecord(Long id) {
        // 校验存在
        validateAccessRecordExists(id);
        // 删除
        accessRecordMapper.deleteById(id);
    }

    private void validateAccessRecordExists(Long id) {
        if (accessRecordMapper.selectById(id) == null) {
            throw exception(ACCESS_RECORD_NOT_EXISTS);
        }
    }

    @Override
    public AccessRecordDO getAccessRecord(Long id) {
        return accessRecordMapper.selectById(id);
    }

    @Override
    public List<AccessRecordDO> getAccessRecordList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return ListUtil.empty();
        }
        return accessRecordMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<AccessRecordDO> getAccessRecordPage(AccessRecordPageReqVO pageReqVO) {
        return accessRecordMapper.selectPage(pageReqVO);
    }

    @Override
    public List<AccessRecordDO> getAccessRecordList(AccessRecordExportReqVO exportReqVO) {
        return accessRecordMapper.selectList(exportReqVO);
    }

}
